<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-goods"></i> 拼团管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>分类列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="12">
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            class="handle-del mr10"
                            @click="delAllSelection"
                        >批量删除</el-button>
                        <el-button
                            type="primary"
                            icon="el-icon-circle-plus-outline"
                            class="handle-del mr10"
                            @click="handleEdit(-1,null)"
                        >添加分类</el-button>
                    </el-col>
                    
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="60" sortable align="center"></el-table-column>
                <el-table-column label="分类名" prop="title" align="center"></el-table-column>
                
                <el-table-column label="商品"  align="center">
                    <template slot-scope="scope">
                          <el-popover
                              placement="bottom"
                              title="商品"
                              width="200"
                              trigger="click"
                              :content="scope.row.goods_name"
                          >
                              <el-link type="primary" slot="reference" :underline="false">查看</el-link>
                          </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="status" align="center">
                  <template slot-scope="scope">
                    <el-tag v-if="scope.row.status == 1">
                        开启
                    </el-tag>
                    <el-tag v-else>
                        关闭
                    </el-tag>
                  </template>
                </el-table-column>
                
                <el-table-column label="操作" width="180" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            @click="updateStatus(scope.row.id,scope.$index,scope.row.status)"
                            :icon="scope.row.status==1?'el-icon-lx-attentionforbid':'el-icon-lx-attention'"
                            type="text"
                            :style="{color:scope.row.status==0?'#67C23A':'#E6A23C'}"
                        >{{ scope.row.status==1?'关闭':'开启' }}</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    :hide-on-single-page="true"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 添加编辑弹出 -->
        <el-dialog v-dialogDrag :title="formName" :visible.sync="editVisible" width="1000px">
            <el-form :model="edit" label-width="70px">
                <el-form-item label="名称" class="form-item">
                    <el-input
                        type="text"
                        v-model="edit.title"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入名称"
                    ></el-input>
                </el-form-item>
                <el-form-item label="指定商品" class="form-item">
                    <el-select v-model="edit.goods" multiple placeholder="选择指定商品" style="width:100%;margin-right:10px">
                       <template v-for="(item) in goodsAll">
                            <el-option
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                               
                            ></el-option>
                        </template>
                    </el-select>
                    
                </el-form-item>
                <el-form-item label="排序" class="form-item">
                    <el-input
                        v-model="edit.sort"
                        maxlength="2"
                        minlength="1"
                        type="number"
                        placeholder="请输入排序"
                    ></el-input>
                </el-form-item>
                <el-form-item label="是否开启" prop="status">
                    <el-switch v-model="edit.status" active-value="1" inactive-value="0"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <div class="dialog-footer-but">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleSave">确 定</el-button>
                </div>
            </span>
        </el-dialog>
        <!-- 预览图片 -->
        <div class="previews" @click="closePreviews" v-if="windowsPopVisible">
            <img :src="windowsPopUrl" title="点击即可关闭预览" />
        </div>
    </div>
</template>

<script>

export default {
  name: 'goods',
  data () {
    return {
      query: {
        name: '',
        page: 1,
        pageSize: 10,
        status: '',
        sort: '1'
      },
      formName: '添加商品',
      editVisible: false,
      windowsPopVisible: false,
      windowsPopUrl: '',
      loading: false,
      tableData: [],
      multipleSelection: [],
      pageTotal: 0,
      goodsAll: [],
      delList: [],
      edit: { to_goods: '' },
      idx: -1,
      id: -1,
      //获取列表请求
      getList: function () {
        let that = this;
        this.api.get('pcate', {} ,res => {
         
          that.tableData = res.data;
        }, undefined, undefined, this);
      },
      //删除请求
      delLists: function (ids, msg) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.post('deleteAction', { ids: ids }, res => {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(msg);
          }, undefined, undefined, this);
        }).catch(() => { });
      }
    };
  },
  watch: {

  },
  created () {
    this.api.get('getGoodsAll', {}, res => {
      this.goodsAll = res.data.list;
    });
    this.getList();
  },
  methods: {
    //设置商品属性
    goodsChange (id, index, key) {
      this.api.put('setGoods', { id: id, key: key }, res => {
        this.$message.success(res.msg);
        this.tableData[index][key] = res.data.val;

      });
    },

    handleEdit (index, row) {
      if (index < 0) {
        this.formName = '添加分类';
        this.edit = { };
      } else {
        this.formName = '编辑分类';
        if(typeof row.goods != 'object'){
          row.goods = row.goods.split(',')
        }
        row.status = row.status+'';
        this.edit = row;
        
      }
      this.editVisible = true;
    },

    handleSave () {
      if (this.edit && this.edit.id) {
        this.api.put('addpcate', this.edit, res => {
          this.$message.success(res.msg);
          this.editVisible = false;
          this.getList();
        });
      } else {
        this.api.put('addpcate', this.edit, res => {
          this.$message.success(res.msg);
          this.editVisible = false;
          this.getList();
        });
      }
    },

    /***
     * 超出上传限制事件
     */
    uploadExceed () {
      this.$message.error('只能上传6张图片');
    },

    /***
     * 添加banner预览
     */
    uploadPreview (file) {
      this.windowsPopVisible = true;
      this.windowsPopUrl = file.response.data.url;
    },

    /***
     * 关闭预览
     */
    closePreviews () {
      this.windowsPopVisible = false;
    },

    /***
     * 添加banner上传成功事件
     */
    uploadSuccess (response, file, fileList) {
      console.log(fileList)
      if (response.code != 1000) {
        this.$message.error(response.msg);
        return false;
      }
      let banner = [];
      for (let i in fileList) {
        banner[banner.length] = fileList[i].response.data.url;
      }
      this.edit.banner = banner;
    },
    imagesUploadSuccess (response, file, fileList) {
      if (response.code != 1000) {
        this.$message.error(response.msg);
        return false;
      }
      let images = [];
      for (let i in fileList) {
        images[images.length] = fileList[i].response.data.url;
      }
      this.edit.images = images;
    },
    

    /**
     * 操作状态
     */
    updateStatus (id, index, status) {
      let str = '';
        str = status == 1 ? '关闭' : '开启';
      this.$confirm('确定要' + str + '吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.api.post('statusAction', { id: id, status: status }, res => {
          this.$message.success(res.msg);
          this.getList();
        });
      }).catch(() => { });

    },


    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    // 删除操作
    handleDelete (index, row) {
      this.delLists([row.id], '删除成功');
    },
    //批量删除
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delLists(ids, `删除了${str}`);
    },

    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    },




  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.t-tag {
    cursor: pointer;
}
</style>
